#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import logging

from net.netserv import EventType
from net.netserv import MsgCtx
from net.netserv import NetAdapter
from net.netserv import NetServer

logging.getLogger().setLevel(logging.INFO)


class PushAdapter(NetAdapter):

    def __init__(self):
        pass

    def handle_data(self, handler, data):
        logging.debug(data)
        ctx = MsgCtx.ctx_from_json(data)
        if ctx.cmd == EventType.DATA:
            self.echo(handler=handler, data=ctx.data)

    def echo(self, handler, data):
        handler.send_cmd(cmd=EventType.DATA, data='Hello {}'.format(data))


if __name__ == '__main__':
    NetServer(host='localhost', port=10112, adapter=PushAdapter()).start()
